var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp()
Page({
  data: {
    // text:"这是一个页面"
    topicList: [],
    page: 1,
    size: 10,
    count: 0,
    scrollTop: 0, type:"",
    showPage: false
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.getTopic();
this.setData({
  type: wx.getStorageSync("type")
})
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  nextPage: function (event) {
    console.log();
    var that = this;
    if (this.data.page + 1 > that.data.count / that.data.size) {
      return true;
    }


    that.setData({
      "page": parseInt(that.data.page) + 1
    });

    this.getTopic();

  },
  getTopic: function () {

    let that = this;
    that.setData({
      scrollTop: 0,
      showPage: false,
      topicList: []
    });
    //页面渲染完成
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 2000
    });

    util.request(api.TopicList, { page: that.data.page, size: that.data.size }).then(function (res) {
      if (res.code === '200') {
        var topicList = res.datas;
        for (var i = 0; i < topicList.length; i++) {
          if (topicList[i].typeId == '1') {
            topicList[i]['url'] = "/pages/sceneTicketDetail/scenticket?id=" + topicList[i].valueId
          } else if (topicList[i].typeId == '0') {
            topicList[i]['url'] = "/pages/hotelRoomDetail/roominfo?id=" + topicList[i].valueId
          } else {
            topicList[i]['url'] = "/pages/tourDetail/travelline?id=" + topicList[i].valueId
          }
        }

        that.setData({
          scrollTop: 0,
          topicList: res.datas,
          showPage: true,

        });
      }
      wx.hideToast();
    });

  },
  prevPage: function (event) {
    if (this.data.page <= 1) {
      return false;
    }

    var that = this;
    that.setData({
      "page": parseInt(that.data.page) - 1
    });
    this.getTopic();
  },
  onPullDownRefresh: function (e) {

  },
  onReachBottom: function (e) {
  }, 
  fnShare: function (e) {
    var that = this;
    var path = "";
    var type = e.currentTarget.dataset.type;
    var goodsId = e.currentTarget.dataset.id;
    var imgUrl = e.currentTarget.dataset.url;
    var goodsName = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    var price = e.currentTarget.dataset.price;
    var marketPrice = e.currentTarget.dataset.marketprice;
    if (type == 'hotel') {
      path = "/pages/hotelDetail/hoteldetail";
    } else if (type == "scene") {
      path = "/pages/sceneDetail/sceninfo";
    } else {
      path = "/pages/tourDetail/travelline"
    }
    app.fnShareGoods(path, goodsId, imgUrl, goodsName, type, address, price,marketPrice);
  }
})